package com.example.easyscript.service.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.easyscript.dto.Req.AddImageReq;
import com.example.easyscript.entity.task.GroupAndImage;
import com.example.easyscript.mapper.GroupAndImageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author huzixian
 * @date 2023-03-08
 */
@Slf4j
@Service
public class GroupAndImageService extends ServiceImpl<GroupAndImageMapper, GroupAndImage> {

    public void addGroupImage(AddImageReq req) {
        QueryWrapper<GroupAndImage> wrapper = new QueryWrapper();
        wrapper.eq("group_id", req.getId());
        List<GroupAndImage> list = this.list(wrapper);

        Set<Integer> idSet = list.stream().map(item->item.getImageId()).collect(Collectors.toSet());

        List<Integer> imageIds = req.getImageIds();
        imageIds.forEach(item -> {
            if(!idSet.contains(item)){
                GroupAndImage groupAndImage = new GroupAndImage();
                groupAndImage.setImageId(item);
                groupAndImage.setGroupId(req.getId());
                this.save(groupAndImage);
                idSet.add(item);
            }
        });
    }
}
